/*
 * Package il.ac.biu.cs.grossmm.impl.activeData
 * File MemNodeManpipulator.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.impl.activeData;

import java.util.logging.Level;
import java.util.logging.Logger;

import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.*;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;

public class MemNodeManpipulator extends MemInspector implements
		NodeManipulator {
	static Logger logger = Logger.getLogger("*MMN*");

	// static {
	// logger.setLevel(Level.FINE);
	// }

	public MemNodeManpipulator() {
		super(1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see il.ac.biu.cs.grossmm.api.activeData.NodeManipulator#createSubnode(il.ac.biu.cs.grossmm.api.activeData.Node,
	 *      il.ac.biu.cs.grossmm.api.activeData.NodeSet)
	 */
	public <N, S> Node<S> createSubnode(Node<N> node, Nodeset<N, S> nodeSet) {
		if (logger.isLoggable(Level.FINE))
			logger.log(Level.FINE, "{0} {1}", new Object[] { node, nodeSet });

		MemNode<N> n = (MemNode<N>) node;

		return n.createSubnode(nodeSet);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see il.ac.biu.cs.grossmm.api.activeData.NodeManipulator#removeSubnode(il.ac.biu.cs.grossmm.api.activeData.Node,
	 *      il.ac.biu.cs.grossmm.api.activeData.NodeSet,
	 *      il.ac.biu.cs.grossmm.api.activeData.Node)
	 */
	public <S> void removeSubnode(Node<S> subnode) {
		if (logger.isLoggable(Level.FINE))
			logger.log(Level.FINE, "{0}", subnode);

		MemNode sn = (MemNode) subnode;

		sn.remove();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see il.ac.biu.cs.grossmm.api.activeData.NodeManipulator#setValue(il.ac.biu.cs.grossmm.api.activeData.Node,
	 *      il.ac.biu.cs.grossmm.api.activeData.Property, java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	public <N, V> void setValue(Node<N> node, Property<N, V> property, V value)
			throws OperationFailedException {
		if (logger.isLoggable(Level.FINE))
			logger.log(Level.FINE, "{0} {1} = {2}", new Object[] { node,
					property, value });

		MemNode<N> n = (MemNode<N>) node;

		n.setValue(property, value);
	}

	public <N, V> void setAndCheck(Node<N> node, Property<N, V> property,
			V value) throws OperationFailedException {
		MemNode<N> n = (MemNode<N>) node;

		if (!n.hasValue(property, value))
			setValue(node, property, value);
	}

	public <N> void copyNode(NodeInspector inspector, Node<N> src, Node<N> dst) {
		// TODO Implement copyNode
		// 
		throw new RuntimeException("Not implemented");
	}

}
